package Nonogram;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/**
 * PanelClueRow
 * @author Rachel Wedewer,
 * Date Modified: 4/30/2014
 * 
 * An important component of the GUI view.java.  This Jpanel represents the row clues that 
 * the user uses to solve the puzzle.  The row clues are represented by pulling out all the
 * elements from CluesArray rowClues (from Data) by rowClues[line] and putting all the elements into one
 *  string with commas between clues. Then adding that string as a JLabel to a cell
 *  of the grid layout.  The font of the JLabel changes depending on what the size of the puzzle.
 *  Also depending on what cell in the PanelPuzzleGrid class is hovered over will highlight the corresponding
 *  cell in this class.  This grid layout is 1xN (N is the board size).
 * 
 */
public class PanelClueRow extends JPanel{
	
	private static Color HIGHTLIGHT = new Color(162,220,218);//Color(red, green, blue);
	private Color panelColor;//Color(red, green, blue);
	
	private int boardsize;
	private CluesArray clues;
	private JPanel cells[];
	private int lineHightlight;
	
	public PanelClueRow(CluesArray rowClues) {
		this.boardsize = rowClues.getClues().length;
		this.clues = rowClues;
		this.setLayout(new GridLayout(boardsize, 1,5,5));
		this.setBackground(Color.BLACK);
		this.cells = new JPanel[this.boardsize];
		lineHightlight = 0;
		updateGUIPuzzle();
	}
	
	//For when a new puzzle is selected
    public void updateGUIPuzzle(){
    	this.removeAll();
    	createGirdCells();    	
    }

    public void createGirdCells(){
    	String cluesForLine="";
    	
    	for(int y=0;y<boardsize; y++){
    		for(int x =0; x< clues.getClues()[y].length; x++){
    			
    			cluesForLine= cluesForLine + clues.getClues()[y][x]+ ", ";
    			
    			}
    		cluesForLine = cluesForLine.substring(0, cluesForLine.length()-2);
    		
    		cells[y] =new JPanel();
    		cells[y].setLayout(new BorderLayout());
    		cells[y].setBorder(new EmptyBorder(0, 10, 0, 10) );
    		JLabel label =new JLabel(cluesForLine);
    		label=increaseFontSize(label);
    		cells[y].add(label, BorderLayout.LINE_END);
    		this.add(cells[y]);
    		cluesForLine="";
    			
    		}
    	
    	panelColor =cells[0].getBackground();
    	}
    
    //Used to better adapt to different size puzzles
    private JLabel increaseFontSize(JLabel label){
    	Font f;
    	if (boardsize > 25){
    		f = new Font("Dialog", Font.BOLD, 15);
    	}
    	else if (boardsize > 15){
    		f = new Font("Dialog", Font.BOLD, 20);
    	}
    	else if (boardsize > 10){
    		f = new Font("Dialog", Font.BOLD, 25);
    	}
    	else{
    		f = new Font("Dialog", Font.BOLD, 32);
    	}
    	label.setFont(f);
    	return label;
    }
    
    public void colorClue(int line){
    	if (line != lineHightlight){
    	cells[lineHightlight].setBackground(panelColor);
    	cells[line].setBackground(HIGHTLIGHT);
    	lineHightlight = line;}
    }
    
    //Important for size panels in view.
    public int getMaxLineClues(){
        int max =0;
        for(int y=0;y<boardsize; y++){
                if (clues.getClues()[y].length> max){
                        max = clues.getClues()[y].length;
                }
        }
        return max;
    }
}
